<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="80px">
			<div class="app-space-between">
				<div class="app-space-between-left">
					<el-form-item label="活动名称:" prop="configName">
						<el-input v-model="queryParams.configName" placeholder="请输入活动名称" clearable @clear="handleQuery"
							@keyup.enter.native="handleQuery" />
					</el-form-item>
					<el-form-item label="状态:" prop="status">
						<el-select v-model="queryParams.status" @change="handleQuery" placeholder="请选择状态" clearable>
							<el-option v-for="dict in dict.type.hotel_statistics_status" :key="dict.value"
								:label="dict.label" :value="dict.value" />
						</el-select>
					</el-form-item>
				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col> -->
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="活动名称" align="center" prop="configName" />
			<el-table-column label="活动类型" align="center" prop="activityDesc" />
			<!-- <el-table-column label="关联店铺" align="center" prop="storeNames" />
			<el-table-column label="关联商品" align="center" prop="goodsNames" /> -->
			<el-table-column label="活动状态" align="center">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.hotel_statistics_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="排序号" align="center" prop="sortNumber" />
			<el-table-column label="创建时间" width="140" align="center">
				<template slot-scope="scope">
					{{scope.row.createTime}}
				</template>
			</el-table-column>

			<el-table-column label="操作" fixed="right" width="120" align="center">
				<template slot-scope="scope">
					<el-link type="primary" :underline="false" style="margin-right: 12px;"
						@click="handleUpdate(scope.row)">编辑</el-link>
					<el-link type="primary" :underline="false" @click="handleDelete(scope.row)">删除</el-link>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改岗位对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :close-on-click-modal="false">
			<div style="padding-right: 40px;">
				<el-form ref="form" :model="form" :rules="rules" label-width="120px">
					<el-row :gutter="20">
						<el-col :span="24">
							<el-form-item label="活动名称:" prop="configName">
								<el-input v-model="form.configName" placeholder="请输入活动名称" />
							</el-form-item>
						</el-col>
					</el-row>
					<el-row :gutter="20">
						<el-col :span="24">
							<el-form-item label="活动类型:" prop="activityType">
								<el-radio-group v-model="form.activityType">
									<el-radio v-for="dict in activityTypeData" :key="dict.value" :label="dict.value">
										{{dict.label}}
									</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- <el-row :gutter="20">
						<el-col :span="24">
							<el-form-item label="活动描述:" prop="activityDesc">
								<quill-edit v-model="form.activityDesc" :height="300"></quill-edit>
							</el-form-item>
						</el-col>
					</el-row> -->

					<el-row :gutter="20">
						<el-col :span="24">
							<el-form-item label="图片:" prop="logoPicAddress">
								<image-upload :isShowTip="true" :height="98" :width="354" v-model="form.logoPicAddress"
									:limit="1">
								</image-upload>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row :gutter="20">
						<el-col :span="12">
							<el-form-item label="状态:" prop="status">
								<el-radio-group v-model="form.status">
									<el-radio v-for="dict in dict.type.hotel_statistics_status" :key="dict.value"
										:label="dict.value">{{dict.label}}</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row :gutter="20">
						<el-col :span="12">
							<el-form-item label="排序号:" prop="sortNumber">
								<el-input v-model.number="form.sortNumber" placeholder="请输入排序号" />
							</el-form-item>
						</el-col>
					</el-row>

					<el-row :gutter="20" v-if="form.activityType == 3">
						<el-col :span="24">
							<el-form-item label="链接图片:" prop="jumpLink">
								<image-upload :isShowTip="true" :height="500" :width="500" v-model="form.jumpLink"
									:limit="1">
								</image-upload>
							</el-form-item>
						</el-col>
					</el-row>

					<div v-if="form.activityType == 2">
						<el-row :gutter="20">
							<el-col :span="12">
								<el-form-item label="商品种类:" prop="storeIds">
									<el-select v-model="form.storeIds" placeholder="请选择商品种类"
										@change="handleGetGoodsList">
										<el-option :label="item.label" :value="item.value"
											v-for="item in dict.type.activity_product_type" :key="item.value" />
									</el-select>
									<i class="el-icon-loading" style="font-size: 14px;margin-left: 8px;" v-if="showSelectLoading"></i>
								</el-form-item>
							</el-col>
						</el-row>
						<el-row :gutter="20" v-if="form.storeIds!=6&&form.storeIds!=7&&form.storeIds!=4">
							<el-col :span="12">
								<el-form-item label="商品:" prop="goodsIds">
									<el-select v-model="form.goodsIds" placeholder="请选择商品" clearable>
										<el-option :label="item.goodsName" :value="item.goodsId"
											v-for="(item,index) in goodsData" :key="index" />
									</el-select>
								</el-form-item>
							</el-col>
						</el-row>
					</div>

					<div v-if="form.activityType == 1">
						<el-row :gutter="20">
							<el-col :span="24">
								<el-form-item label="RSA 签名 accessKey:" prop="f1">
									<el-input v-model="form.f1" type="textarea" rows="4" placeholder="请输入签名" />
								</el-form-item>
							</el-col>
						</el-row>

						<el-row :gutter="20">
							<el-col :span="24">
								<el-form-item label="RSAPublicKey公钥:" prop="f2">
									<el-input type="textarea" v-model="form.f2" rows="4" placeholder="请输入公钥" />
								</el-form-item>
							</el-col>
						</el-row>

						<el-row :gutter="20">
							<el-col :span="24">
								<el-form-item label="RSASignPrivateKey私钥:" prop="f3">
									<el-input v-model="form.f3" type="textarea" rows="4" placeholder="请输入私钥" />
								</el-form-item>
							</el-col>
						</el-row>
					</div>
				</el-form>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm" :loading="btnLoading">{{form.configId?'保 存':'确 定'}}
				</el-button>
				<el-button @click="cancel" :disabled="btnLoading">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		restaurantList,
		restaurantAdd,
		restaurantEdit,
		restaurantDetails,
		restaurantDelete
	} from "@/api/runSetup/content";
	import {
		listWcGoods,
	} from "@/api/culturalCreativity/shopping.js";
	import {
		goodsList
	} from "@/api/shopping/content";
	import {
		storeList
	} from '@/api/store/content'
	import QuillEdit from "@/components/Editor";
	export default {
		dicts: ['hotel_statistics_status', 'activity_product_type', 'product_type'],
		components: {
			QuillEdit
		},
		data() {
			return {
				activityTypeData: [{
						label: "福礼商城",
						value: 1
					},
					{
						label: "热销商品",
						value: 2
					},
					{
						label: "图片宣传",
						value: 3
					},
				],
				// 遮罩层
				loading: true,
				showSelectLoading:false,
				btnLoading: false,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				diLogLoading: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					configType: 0,
					configName: undefined,
					status: undefined
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					configName: {
						required: true,
						message: "请输入活动名称",
						trigger: "blur"
					},
					logoPicAddress: {
						required: true,
						message: "请上传轮播图片",
						trigger: "blur"
					},
					activityDesc: {
						required: true,
						message: "请输入活动描述",
						trigger: "blur"
					},
					jumpLink: {
						required: true,
						message: "请上传链接图片",
						trigger: "change"
					},
					storeIds: {
						required: true,
						message: "请选择商品种类",
						trigger: "blur"
					},
					goodsIds: {
						required: false,
						message: "请选择商店",
						trigger: "blur"
					},
					status: {
						required: true,
						message: "请选择状态",
						trigger: "blur"
					},
					sortNumber: {
						required: true,
						validator:(rule, value, callback)=>{
							let reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g;
							let blo = reg.test(value);
							if(!value) return callback(new Error('请输入排序号'));
							if(!blo){
								console.log("这个区域还是能走出来")
								return callback(new Error('请输入数字'));
							};
							return callback();
						},
						trigger: "blur"
					},
					activityType: {
						required: true,
						message: "请选择活动类型",
						trigger: "change"
					},
					f1: {
						required: true,
						message: "请输入签名",
						trigger: "blur"
					},
					f2: {
						required: true,
						message: "请输入公钥",
						trigger: "blur"
					},
					f3: {
						required: true,
						message: "请输入私钥",
						trigger: "blur"
					}
				},
				goodsData: [],
				storeData: []
			};
		},
		async created() {
			let data = {
				pageNum: 1,
				pageSize: 9999999,
			};
			let response2 = await storeList(data);
			this.storeData = response2.rows;

			this.getList();
		},
		methods: {
			async handleGetGoodsList(e) {
				try {
					let data = {
						pageNum: 1,
						pageSize: 9999999,
					};
					this.form.goodsIds = "";
					this.goodsData = [];
					this.showSelectLoading = true;
					let response1 = null;
					if (Number(e) == 3) { //酒店
						response1 = await goodsList(data);
					}
					if (Number(e) == 1) { //文创
						response1 = await listWcGoods(data);
					}
					if (Number(e) == 2) { //服化
						// response1 = await goodsList(data);
					}
					if (Number(e) == 5) { //门票
						this.dict.type.product_type
						response1 = {
							rows: [...this.dict.type.product_type].map(item => {
								return {
									goodsName: item.label,
									goodsId: item.value
								}
							})
						};
					}
					this.showSelectLoading = false;
					if (!response1) return;
					this.goodsData = response1.rows;
				} catch (e) {
					console.error(e);
					this.showSelectLoading = false;
					//TODO handle the exception
				}
			},
			/** 查询岗位列表 */
			getList() {
				this.loading = true;
				restaurantList({
					...this.queryParams,
					configType: 3
				}).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					configType: 0,
					configName: undefined,
					urlAddress: undefined,
					uniappRoute: undefined,
					logoPicAddress: undefined,
					activityDesc: undefined,
					storeIds: undefined,
					goodsIds: undefined,
					status: 0,
					sortNumber: undefined,
					activityType: 1,
					jumpLink: undefined,
					f1: undefined,
					f2: undefined,
					f3: undefined
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.configId)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.title = "新增";
				this.open = true;
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				let postId = row.configId || this.ids
				if (Array.isArray(postId)) postId = postId.toString()
				this.title = "编辑";
				this.open = true;
				this.diLogLoading = true
				restaurantDetails(postId).then(response => {
					if(Number(response.data.activityType) == 2){
						this.handleGetGoodsList(response.data.storeIds);
					};
					for (let i in this.form) this.form[i] = response.data[i]
					this.form.configId = postId;
					this.form.activityType = Number(this.form.activityType);
					this.form.goodsIds = Number(this.form.goodsIds);
					this.diLogLoading = false;
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.btnLoading = true;
						let form = JSON.parse(JSON.stringify(this.form));
						// 增加商品 店铺名字
						this.goodsData.forEach(item => {
							if (item.goodsId == form.goodsIds) form.goodsNames = item.goodsName
						});
						this.storeData.forEach(item => {
							if (item.storeId == form.storeIds) form.storeNames = item.storeName
						});
						// form.activityType
						form.activityDesc = this.activityTypeData.reduce((pev, next) => {
							if (next.value == form.activityType) {
								pev = next.label;
							}
							return pev;
						}, "");
						if (form.configId != undefined) {
							restaurantEdit(form).then(response => {
								this.$modal.msgSuccess("编辑成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						} else {
							restaurantAdd({
								...form,
								configType: 3
							}).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.configId || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString()
				this.$modal.confirm('此操作将要删除所选的内容，是否要继续？').then(function() {
					return restaurantDelete(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('system/post/export', {
					...this.queryParams
				}, `post_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
